import 'dart:convert';
import 'dart:ui';

import 'package:flutter/foundation.dart';
import 'package:flutter_yn_goods/utils/shared/shared_export.dart';

import 'package:shared_preferences/shared_preferences.dart';

class SharedStorage {
  static final SharedStorage _instance = SharedStorage._();

  factory SharedStorage() => _instance;

  SharedStorage._();

  late SharedPreferences prefs;

  Future initShareStorage() async {
    prefs = await SharedPreferences.getInstance();
  }

  //第一次启动
  Future<bool> firstLaunched() {
    return prefs.setBool(SharedKey.firstLaunch, false);
  }

  //是否是第一次启动
  bool isFirstOpen() {
    return prefs.getBool(SharedKey.firstLaunch) ?? true;
  }

  //第一次启动
  String? getDeviceId() {
    return prefs.getString(SharedKey.deviceId);
  }

  Locale? initialLocale() {
    String? langCode = prefs.getString(SharedKey.langCode);
    String? countryCode = prefs.getString(SharedKey.countryCode);
    if (langCode != null && countryCode != null) {
      return Locale(langCode, countryCode);
    } else {
      return null;
    }
  }

  setLocale(Locale locale) {
    prefs.setString(SharedKey.langCode, locale.languageCode);
    prefs.setString(SharedKey.countryCode, locale.countryCode ?? '');
  }
}
